﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

namespace EducationManagementSystem.Zr
{
    public partial class CourseInfoMange : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Bind();//标题
                Bind2();
            }
        }

        private void Bind()
        {
            SqlConnection conn = DB.Conn();
            conn.Open();
            string sql = "select a.schoolname from School a,Zhuren b where zhurenID='" + Session["zhurenid"].ToString() + "' and a.schoolid=b.schoolid";
            SqlCommand cmd = new SqlCommand(sql, conn);
            DataSet ds = new DataSet();
            SqlDataAdapter sda = new SqlDataAdapter(cmd);
            sda.Fill(ds);
            this.TdTitle.InnerHtml = ds.Tables[0].Rows[0][0].ToString() + "的课程列表";//标题

        }

        private void Bind2()
        {
            SqlConnection conn = DB.Conn();
            conn.Open();
            string sql = "select a.courseid,a.coursename,a.courseCount from course a,zhuren b where zhurenID='" + Session["zhurenid"].ToString() + "' and a.schoolid=b.schoolid";
            SqlCommand cmd = new SqlCommand(sql, conn);
            DataSet ds = new DataSet();
            SqlDataAdapter sda = new SqlDataAdapter(cmd);
            sda.Fill(ds);
            GridView1.DataSource = ds.Tables[0];
            GridView1.DataBind();

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (TextBox1.Text == "")
            {
                this.Terr.InnerHtml = "<font color='red'>课程名称不能为空</font>";

            }
            else
            {
                SqlConnection conn = DB.Conn();
                conn.Open();
                string sql = "select schoolid from zhuren where zhurenid='" + Session["zhurenid"].ToString() + "'";
                SqlCommand cmd = new SqlCommand(sql, conn);
                DataSet ds = new DataSet();
                SqlDataAdapter sda = new SqlDataAdapter(cmd);
                sda.Fill(ds);
                string sid = ds.Tables[0].Rows[0][0].ToString();//获取学院编号



                string sql2 = "insert into course(coursename,coursecount,schoolid) values('" + TextBox1.Text + "','" + TextBox2.Text + "','" + sid + "')";
                SqlCommand cmd2 = new SqlCommand(sql2, conn);
                int c = cmd2.ExecuteNonQuery();
                if (c > 0)
                {
                    this.Page.RegisterStartupScript(" ", "<script>alert('添加成功'); </script> ");
                    Bind2();
                }
                else
                {

                    this.Page.RegisterStartupScript(" ", "<script>alert('添加失败 '); </script> ");
                }
            }
        }

        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView1.PageIndex = e.NewPageIndex;
            Bind2();
        }

        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "de")
            {
                string cid = e.CommandArgument.ToString();

                SqlConnection conn = DB.Conn();
                conn.Open();
                string sql = "delete from course where courseid='" + cid + "'";
                SqlCommand cmd = new SqlCommand(sql, conn);
                int a = cmd.ExecuteNonQuery();
                if (a > 0)
                {
                    this.Page.RegisterStartupScript(" ", "<script>alert('删除成功'); </script> ");
                    Bind2();
                    this.Terr.InnerHtml = "";
                }
                else
                {
                    this.Page.RegisterStartupScript(" ", "<script>alert('删除失败'); </script> ");
                }
            }
        }




    }
}